/**
 * // Definition for a Node.
 * function Node(val,children) {
 *    this.val = val;
 *    this.children = children;
 * };
 */

/**
 * @param {Node|null} root
 * @return {number[][]}
 */
export var levelOrder = function (root) {
  if (!root) return []
  const ans = []
  const queue = [root]
  while (queue.length) {
    const n = queue.length
    const level = []
    for (let i = 0; i < n; i++) {
      const cur = queue.shift()
      level.push(cur.val)
      if (cur.children) {
        for (const child of cur.children) {
          queue.push(child)
        }
      }
    }
    ans.push(level)
  }
  return ans
}
